#!/bin/bash -x

kata_coco_top_dir="$HOME/workspace/CoCo/"

kata_containers_repo_url="https://gitee.com/hanliyang-kata-coco/kata-containers.git"
kata_containers_repo_branch="3.13.0-hygon-arch-sig"
kata_containers_repo_dir="${kata_coco_top_dir}/kata-containers"

if [ ! -e ${kata_containers_repo_dir} ]; then
  git clone ${kata_containers_repo_url} \
    -b ${kata_containers_repo_branch} \
    ${kata_containers_repo_dir}
fi

# check and install yq if not exist
echo "Check and Install yq"
command yq > /dev/null
if [ $? -ne 0 ]; then
  DEBUG="y" ${kata_containers_repo_dir}/ci/install_yq.sh
  echo 'export PATH=$PATH:$HOME/go/bin/' >> $HOME/.bashrc
  source $HOME/.bashrc
fi

kernel_version="6.6.71-openanolis"
kernel_repo_url="https://gitee.com/anolis/cloud-kernel.git"
kernel_repo_branch="devel-6.6"
kernel_repo_commit="ab224dde9d0e07d1aa69f26c2b3b63b3b37004f5"
kernel_repo_dir="/dev/shm/openanolis-kernel"
kernel_builder_dir="${kata_containers_repo_dir}/tools/packaging/kernel"

kernel_revert_commits=( \
  "ad91a2dacbf8c26a446658cdd55e8324dfeff1e7"
  )

LUKS_SECURE_MOUNT=0
if [ $LUKS_SECURE_MOUNT -eq 1 ]; then
  secure_mount="-m"
else
  secure_mount=""
fi

if [ ! -e ${kernel_repo_dir} ]; then
  git clone ${kernel_repo_url} \
    -b ${kernel_repo_branch} \
    ${kernel_repo_dir}
fi
pushd ${kernel_repo_dir}
git reset --hard ${kernel_repo_commit}
# anolis kernel-6.6 needs revert some commits to support kata & coco
git config user.email "kata-coco-builder" && \
git config user.name "kata-coco-builder"
for commit in ${kernel_revert_commits[@]}; do
  git diff ${commit} ${commit}~1 > patch.diff.revert.${commit}
  git apply patch.diff.revert.${commit}
  git status | grep modified: | awk '{print $2}' | xargs -n1 git add
  git status | grep deleted: | awk '{print $2}' | xargs -n1 git rm
  git status -s | grep "^??" | awk '{print $2}' | xargs -n1 git add
  git commit -s -m "revert ${commit}"
done
popd # ${kernel_repo_dir}

install_kata_guest_kernel_deps () {
  echo "Install kata guest kernel dependencies"
  sudo yum install -y \
    make gcc gcc-c++ \
    flex bison openssl-devel \
    elfutils-libelf-devel \
    rpm-build perl dwarves
}

install_kata_guest_kernel_deps

build_install_guest_kernel () {
  echo "Build and Install kata guest kernel"

  pushd ${kernel_builder_dir}

  ./build-kernel.sh -a x86_64 \
    -d -x -f \
    ${secure_mount} \
    -k $kernel_repo_dir setup

  ./build-kernel.sh -a x86_64 \
    -d -x -f \
    ${secure_mount} \
    -k $kernel_repo_dir build

  #./build-kernel.sh -a x86_64 \
  #  -d -x -f \
  #  ${secure_mount} \
  #  -H rpm \
  #  -k $kernel_repo_dir build-headers
  popd # ${kernel_builder_dir}

  kata_config_version=$(cat ${kernel_builder_dir}/kata_config_version)
  vmlinuz="vmlinuz-${kernel_version}-${kata_config_version}-csv"
  vmlinux="vmlinux-${kernel_version}-${kata_config_version}-csv"

  install_path=/opt/kata/share/kata-containers/
  sudo mkdir -p ${install_path} || true
  pushd ${kernel_repo_dir}
  sudo install --mode 0644 -D arch/x86/boot/bzImage $install_path/$vmlinuz
  sudo install --mode 0644 -D vmlinux $install_path/$vmlinux
  sudo install --mode 0644 -D ./.config \
    $install_path/config-${kernel_version}-${kata_config_version}-csv
  popd # ${kernel_repo_dir}
  pushd ${install_path}
  sudo ln -sf $vmlinuz vmlinuz-confidential-csv.container
  sudo ln -sf $vmlinux vmlinux-confidential-csv.container
  popd # ${install_path}
}

build_install_guest_kernel
